package com.example.myapplication;

import android.content.Context;
import android.content.Intent;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.webkit.MimeTypeMap;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

public class FileUtil {

    public static File createFile(String parentPath, String childPath, String extension) {

        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd_hhmmss");
        String fileName = format.format(new Date());
        File storageDir = new File(parentPath, childPath);
        if (!storageDir.exists()) {
            storageDir.mkdir();
        }
        File saveRecorderFile = new File(storageDir, "IMG" + fileName + "." + extension);
        if (saveRecorderFile.exists()) {
            saveRecorderFile.delete();
        }
        return saveRecorderFile;

    }

    public static void showInAlbum(Context context, String path) {

        String[] str = path.split("[.]");
        String mineType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(str[str.length - 1]);
        MediaScannerConnection.scanFile(context, new String[]{path}, new String[]{mineType}, null);
        context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + path)));

    }

}
